<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/10/29
 * Time: 14:28
 */
namespace App\Service\Live;

use App\Constant\RedisKeyConstant;
use Illuminate\Support\Facades\Redis;

class AnchorService extends BaseService
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * admin block user
     * @param string $room_name
     * @param $user_id
     * @return mixed
     */
    public function blockUser($room_name, $user_id)
    {
        return Redis::SADD(RedisKeyConstant::ROOM_BLOCK_USER_PREFIX . $room_name, $user_id);
    }

    /**
     * admin unblock user
     * @param string $room_name
     * @param $user_id
     * @return mixed
     */
    public function unblockUser($room_name, $user_id)
    {
        return Redis::SREM(RedisKeyConstant::ROOM_BLOCK_USER_PREFIX . $room_name, $user_id);
    }

    /**
     * admin get block user
     * @param string $room_name
     * @param bool $nick_name
     * @return mixed
     */
    public function getBlockUser($room_name = '', $nick_name = false)
    {
        $block_users    =   Redis::SMEMBERS(RedisKeyConstant::ROOM_BLOCK_USER_PREFIX . $room_name);
        if (empty($block_users)) {
            return [];
        }

        if (!$nick_name) {
            return  $block_users;
        }

        $user_info =   [];
        foreach ($block_users as $user_id) {
            $user_nick  =   Redis::HGET(RedisKeyConstant::USER_DETAIL_INFO, $user_id);
            $user_name  =   '未知用户';
            if (!empty($user_nick)) {
                $user_detail    =   json_decode($user_nick, true);
                $user_name      =   $user_detail['nick_name'] ?? $user_name;
            }
            $single_info['user_id']     =   $user_id;
            $single_info['nick_name']   =   $user_name ?? '未知用户';
            $user_info[]    =   $single_info;
        }
        return $user_info;
    }

    /**
     * admin get online user
     * @param string $room_name
     * @return array
     */
    public function getOnlineUser($room_name = '')
    {
        $total  =   Redis::SMEMBERS(RedisKeyConstant::ROOM_WATCH_PEOPLE_PREFIX . $room_name);
        if (empty($total)) {
            return [];
        }

        $all_user=  [];
        $client =   new ClientService();
        foreach ($total as $item) {
            $single_user    =   $client->setUser($item);
            $all_user[$item]    =   $single_user['nick_name'] ?? '';
        }

        return $all_user;
    }
}
